<!--
* description: 库存统计
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      v-if="initTemplate"
      ref="multipleTable"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicApiConfig="apiConfig"
      :dynamicTableCols="tableCols"
      :dynamicFormFields="formField"
      :dynamicIsShowSelect="false"
      :dynamicIsColumnDrop="true"
      :callbackObj="callbackObj"
    >
      <template slot="controlBtn" slot-scope="scope">
        <el-button
          type="primary"
          size="mini"
          class="el-icon-edit"
          @click="edit(scope.row.index)"
          >编辑</el-button
        >
      </template>
    </one-table-template>
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import { wmsApis } from "@/api/wms/onStock/api";
import OneTableTemplate from "@/components/wms/templates/oneTable";
import request from "@/utils/request";
export default {
  name: "stockStatistics",
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],
  components: {
    OneTableTemplate
  },
  watch: {
    $route(val) {
      if (
        this.formField.pageId !== "" &&
        this.$route.query.pageId !== undefined &&
        this.$route.query.pageId !== this.formField.pageId
      ) {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
        this.queryTable();
      }
    }
  },
  created() {
    if (this.$route.query != null) {
      // 判断传入query参数，初始化数据
      if (this.$route.query.pageId != null && this.$route.query.pageId !== "") {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
      }
    }
    this.querySelect();
  },
  data() {
    return {
      partNoBefore:'', //备件编号  处理前不加逗号的

      // 网格查询API配置对象
      apiConfig: wmsApis.queryStorageStatistics,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.queryTable(),
          name: "search",
          position: "right",
          text: "查询" //"查询"
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          name: "reset",
          position: "right",
          clickEvent: () => this.reset(),
          text: "重置" //"重置"
        },
        {
          compKey: "btnKey4",
          type: "",
          size: "small",
          clickEvent: () => this.export(),
          name: "export",
          position: "left",
          text: "导出" //"导出"
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "compKey1",
          labelName: "仓库名称",
          codeField: "WAREHOUSE_IDS",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: true,
          options: []
        },
       {
          compKey: "compKey2",
          labelName: "备件编号",
          codeField: "PART_NOS_NEW", //['0','9]
          component: () => import("@/components/org/commonInput/textareaInput"),
          // type: "inputText",
          isMust: true,
        },
        {
          compKey: "compKey3",
          labelName: "供应商",
          codeField: "SUPPLIER_CODE",
          textField: "PARTNER_SHORT_NAME",
          component: () =>
            import("@/components/wms/wmsEjectWindows/wmsSupplier"),
          type: "propus",
          isMust: true,
          isMul: false,
          readonly: true,
          returnCodeField: "SUPPLIER_CODE",
          returnTextField: "PARTNER_SHORT_NAME"
        }
      ],
      // 动态生成网格列
      tableCols: [
        {
          prop: "WAREHOUSE_NAME",
          label: "仓库名称",
          align: "center"
        },
        {
          prop: "PART_NO",
          label: "备件编号",
          align: "center"
        },
        {
          prop: "PART_NAME",
          label: "备件名称",
          align: "center"
        },
        {
          prop: "PARTNER_SHORT_NAME",
          label: "供应商简称",
          align: "center"
        },
        {
          prop: "LP_QTY",
          label: "良品库存",
          align: "center"
        },
        {
          prop: "BLP_QTY",
          label: "不良品库存",
          align: "center"
        },
        {
          prop: "DJ_QTY",
          label: "冻结数量",
          align: "center"
        },
        {
          prop: "YCH_QTY",
          label: "预出库库存",
          align: "center"
        },
        {
          prop: "KY_QTY",
          label: "可用库存",
          align: "center"
        },
           {
          prop: "SHZC_QTY",
          label: "收货暂存数量",
          align: "center"
        },
          {
          prop: "PUR_INTRANSIT_QTY",
          label: "采购在途",
          align: "center"
        },
          {
          prop: "ALL_INTRANSIT_QTY",
          label: "调拨在途",
          align: "center"
        } 
      ],
      //表单查询数据
      formField: {
        WAREHOUSE_IDS: "",
        PART_NO: "",
        WAREHOUSE_IDS_NEW: "",
        SUPPLIER_CODE: "",
        PARTNER_SHORT_NAME: "",
        PUR_INTRANSIT_QTY:"",
        ALL_INTRANSIT_QTY:"",
        PART_NOS_NEW:""
        
      },
      unFormField: {},
      // 控制初始化页面
      initTemplate: false,
       callbackObj: {
        //  当选择项发生变化时会触发该事件
        onCheckSelectChange: (selection) => {
          this.checkTableList = selection;
        },
         onBeforeQuery: queryObj => {
           queryObj.variables.PART_NOS_NEW = queryObj.variables.PART_NOS_NEW.trim()
            .split("\n")
            .toString();
      
        },
      },
    };
    
  },
  methods: {
    querySelect() {
      request({
        url: "/mp/wm/vl/basedata/warehouseuser/queryUserByWHBox.do",
        method: "post",
        data: {
          IS_ENABLE: "1"
        }
      }).then(res => {
        if (res.listRow && res.listRow.length > 0) {
          res.listRow.map(item => {
            this.formField.WAREHOUSE_IDS_NEW += item.WAREHOUSE_ID + ",";
            this.tableComponents[0].options.push({
              label: item.WAREHOUSE_NAME,
              value: item.WAREHOUSE_ID
            });
          });
          this.formField.WAREHOUSE_IDS_NEW = this.formField.WAREHOUSE_IDS_NEW.substr(
            0,
            this.formField.WAREHOUSE_IDS_NEW.length - 1
          );
          this.formField.WAREHOUSE_IDS = res.listRow[0].WAREHOUSE_ID;
        }
        this.initTemplate = true;
        this.$nextTick(() => {
          this.queryTable();
        });
      });
    },
    export() {
      this.partNoBefore = this.formField.PART_NOS_NEW;
      this.formField.PART_NOS_NEW=this.formField.PART_NOS_NEW.trim().split("\n").toString();

      const exportLoading = this.$loading({
        lock: true,
        text: "正在导出...",
        // spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
        fullscreen: true
      });
      request({
        url: "/mp/wm/vl/warehouse/storage/exportStorageStatistics.do",
        method: "post",
        headers: {
          "Cache-Control": "no-cache, no-store,max-age=0, must-revalidate",
          Pragma: "no-cache",
          Expires: "0",
          "Content-Type": "application/json;charset=UTF-8"
        },
        responseType: "blob",
        data: this.formField
      })
        .then(response => {
          exportLoading.close();
          let blob = new Blob([response], { type: "application/vnd.ms-excel" }); // 这里表示xmlx类型
          let downloadElement = document.createElement("a");
          let href = window.URL.createObjectURL(blob); // 创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "库存统计导出.xls"; // 下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        })
         .then(response =>{
        this.formField.PART_NOS_NEW = this.partNoBefore;
        })
        .catch(err => {
          exportLoading.close();
          console.log(err);
        });
    }
  }
};
</script>
